from marajo.appengine.handlers.db import DBHandler
from marajo.exceptions import Unauthorized
import logging
log = logging.getLogger("marajo.db")

class CommentHandler(DBHandler):
	"""Override the POST to allow any user to do a create"""

	def post(self):
		"""Save or update object to the DB"""
		obj = self.read()
		if obj:
			if not self.user:
				raise Unauthorized()
			log.info("====== Update Comment =======")
			obj = self.update(obj, self.request.POST)
			log.info("=============================")
		else:
			log.info("====== Create Comment for: %s =======" % self.request.POST.get("post"))
			obj = self.create(self.request.POST)
			log.info("=============================")
		return self.redirect("posts/%s" % obj.post.id)
